#include "intelligent_community.h"

#include "ui_intelligent_community.h"

intelligent_community::intelligent_community(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::intelligent_community)
{
    ui->setupUi(this);
    initUI();
    PushButtonConnect();
    installEventFilter(this);
}

intelligent_community::~intelligent_community()
{
    delete ui;
}

void intelligent_community::initUI()
{
    this->setWindowFlag(Qt::FramelessWindowHint);
}

bool intelligent_community::eventFilter(QObject *obj, QEvent *event) {
    if (obj == this) {
        if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if (mouseEvent->button() == Qt::LeftButton) {
                // 记录鼠标按下时的位置
                pressPos = mouseEvent->pos();
                // 启动定时器，检测长按
                QTimer::singleShot(500, this, [this]() {
                    if (isLongPress) {
                        // 长按后开始移动窗口
                        this->setCursor(Qt::ClosedHandCursor);
                    }
                });
                isLongPress = true;
            }
        } else if (event->type() == QEvent::MouseMove && isLongPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            // 计算窗口移动的偏移量
            QPoint delta = mouseEvent->pos() - pressPos;
            this->move(this->pos() + delta);
        } else if (event->type() == QEvent::MouseButtonRelease) {
            if (isLongPress) {
                // 释放鼠标时恢复光标并重置状态
                this->setCursor(Qt::ArrowCursor);
                isLongPress = false;
            }
        }
    }
    return QWidget::eventFilter(obj, event);
}

void intelligent_community::PushButtonConnect()
{
    connect(ui->pushButton_i, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->pushButton_e, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pushButton_d, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(2);
    });
    connect(ui->pushButton_n, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(3);
    });
    connect(ui->pushButton_p, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(4);
    });
    connect(ui->pushButton_f, &QPushButton::clicked, this ,[this]() {
        ui->stackedWidget->setCurrentIndex(5);
    });
}

void intelligent_community::on_pushButton_close_clicked()
{
    close();
}

